package com.lzx.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.locks.ReentrantLock;

public class Exec {

	public static void main(String[] args) throws IOException,
			InterruptedException {
		System.out.println(System.getenv());
		for (Entry<String, String> e : System.getenv().entrySet()) {
			System.out.println(e.getKey() + ":" + e.getValue());
		}

		Runtime.getRuntime().exec("ls -al");

		String[] cmdarray = new String[] { "bash", "-c",
				"ls -al | wc -l && sleep 10" };
		Process process = Runtime.getRuntime().exec(cmdarray);

		// System.out.println(getContent(process.getInputStream()));
		//
		// System.err.println(getContent(process.getErrorStream()));
		//
		//
		process.getErrorStream().close();
		process.getInputStream().close();
		process.getOutputStream().close();

		System.out.println(process.waitFor());
		System.out.println(process.exitValue());

		process.getErrorStream().close();
		process.getInputStream().close();
		process.getOutputStream().close();
		process.destroy();

		process.getOutputStream();

	}

	public static List<String> getContent(InputStream is) throws IOException {
		List<String> list = new ArrayList<String>();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line = null;
		while ((line = br.readLine()) != null) {
			list.add(line);
		}
		br.close();
		return list;

	}

}
